﻿using EyFramework.Common;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EyFramework.Library
{
    /// <summary>
    /// 编辑窗体基类
    /// </summary>
    public partial class FrmDevEditBase : FrmDevBase
    {
        private string _ID = String.Empty;

        /// <summary>
        /// 是否强制
        /// </summary>
        private bool _IsQuit = false;

        /// <summary>
        /// 当前显示ID
        /// </summary>
        public string ID
        {
            set
            {
                _ID = value;
            }
            get { return _ID; }
        }

        private List<string> _IDList = new List<string>();

        //所有待展示的ID列表
        public List<string> IDList
        {
            set
            {
                _IDList = value;
            }
            get { return _IDList; }
        }

        public event EventHandler OnDataSaved;

        public FrmDevEditBase()
        {
            InitializeComponent();
        }

        private void dataNavigator1_PositionChanged(object sender, EventArgs e)
        {
            this.ID = IDList[this.dataNavigator1.CurrentIndex];
            DisplayData();
        }

        public virtual void InitForm()
        {
        }

        public virtual void DisplayData()
        {
        }

        /// <summary>
        /// 处理数据保存后的事件触发
        /// </summary>
        public virtual void ProcessDataSaved(object sender, EventArgs e)
        {
            if (OnDataSaved != null)
            {
                OnDataSaved(sender, e);
            }
        }

        /// <summary>
        /// 保存数据（新增和编辑的保存）
        /// </summary>
        public virtual bool SaveEntity()
        {
            bool result = false;
            if (!string.IsNullOrEmpty(ID))
            {
                //编辑的保存
                result = SaveUpdated();
            }
            else
            {
                //新增的保存
                result = SaveAddNew();
            }
            return result;
        }

        /// <summary>
        /// 更新已有的数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveUpdated()
        {
            return true;
        }

        /// <summary>
        /// 保存新增的数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveAddNew()
        {
            return true;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="close">关闭窗体</param>
        private void SaveEntity(bool clearData, bool close)
        {
            // 检查输入的有效性
            if (this.CheckInput())
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    if (this.SaveEntity())
                    {
                        ProcessDataSaved(this.btnOK, new EventArgs());
                        if (close)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            Globals.ShowTips("保存成功!");
                            if (clearData)
                            {
                                this.ClearScreen();
                            }
                            else
                            {
                                this.dataNavigator1.CurrentIndex = this.dataNavigator1.CurrentIndex + 1;
                            }
                        }
                    }
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        /// <summary>
        /// 清理屏幕
        /// </summary>
        public virtual void ClearScreen()
        {
        }

        /// <summary>
        /// 校验输入数据
        /// </summary>
        /// <returns></returns>
        public virtual bool CheckInput()
        {
            return true;
        }

        /// <summary>
        /// 保存并新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            SaveEntity(true, false);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ceContinue.Checked)
            {
                SaveEntity(false, false);//持续操作不关闭窗体
            }
            else
            {
                SaveEntity(false, true);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 校验当前页面上是否所有的修改
        /// </summary>
        /// <returns></returns>
        public virtual bool CheckUpdateSaved()
        {
            return false;
        }

        private void FrmDevEditBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_IsQuit)
            {
                if (CheckUpdateSaved())
                {
                    var result = Globals.ShowYesNoCancelAndTips("您当前界面存在修改的数据未保存，未保存的修改将会丢失，是否保存?");
                    if (result != DialogResult.No)
                    {
                        if (result == DialogResult.Yes)
                        {
                            SaveEntity(false, true);
                            this._IsQuit = true;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
            }
        }

        private void FrmDevEditBase_Load(object sender, EventArgs e)
        {
            InitForm();
            this.dataNavigator1.IndexChanged += new IndexChangedEventHandler(dataNavigator1_PositionChanged);
            if (!string.IsNullOrEmpty(_ID) && _IDList.Count > 1)
            {
                this.dataNavigator1.IDList = _IDList;
                this.dataNavigator1.CurrentIndex = _IDList.IndexOf(_ID);
            }
            else
            {
                this.dataNavigator1.Visible = false;
                this.ceContinue.Visible = false;
            }
            DisplayData();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.PageDown)
            {
                this.dataNavigator1.MoveNext();
            }
            else if (keyData == Keys.PageUp)
            {
                this.dataNavigator1.MovePre();
            }
            else if (keyData == (Keys.Alt | Keys.PageDown))
            {
                this.dataNavigator1.MoveLast();
            }
            else if (keyData == (Keys.Alt | Keys.PageUp))
            {
                this.dataNavigator1.MoveFirst();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}